﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace ClickNET.Imaging
{
    public class FileSystemGalleryManager
    {
        private static readonly string s_xmlDirectory = Config.DefaultInstance.GetConfigString("gallery", "xmlDirectory");

        public static Gallery LoadGallery(string galleryFolder)
        {
            string galleryXmlFilePath = FileSystemGalleryManager.GetGalleryXmlFilePath(galleryFolder);

            if (File.Exists(galleryXmlFilePath))
                return ClickNET.Serialization.SerializationManager.Xml.ObjectFromXMLFile(galleryXmlFilePath, typeof(Gallery)) as Gallery;
            else
            {
                Gallery g = new Gallery(galleryFolder);
                g.ProcessGalleryFolder();

                return g;
            }
        }

        public static bool GalleryXmlFileExists(string galleryFolder)
        {
            string xmlFilePath = FileSystemGalleryManager.GetGalleryXmlFilePath(galleryFolder);

            return File.Exists(xmlFilePath);
        }

        public static string GetGalleryXmlFilePath(string galleryFolder)
        {
            string galleryXmlDir = Path.Combine(Config.DataDirectory, s_xmlDirectory);
            return Path.Combine(galleryXmlDir, galleryFolder + ".xml");
        }

        public static string GetThumbnailFilesystemPath(Picture p, int thumbSize)
        {
            if (null != p)
                return FileSystemGalleryManager.GetThumbnailFilesystemPath(p.FolderName, p.FileName, thumbSize);

            return string.Empty;
        }

        public static string GetThumbnailFilesystemPath(string galleryName, string fileName, int thumbSize)
        {
            string thumbsDir = Path.Combine(Config.DataDirectory, s_thumbsDirectory);
            thumbsDir = Path.Combine(thumbsDir, galleryName);

            return Path.Combine(thumbsDir, String.Format(s_thumbsFilePrefixFormat, thumbSize, fileName));
        }

        public static string GetThumbnailUrl(Picture p, int thumbSize)
        {
            if (null != p)
                return FileSystemGalleryManager.GetThumbnailUrl(p.FolderName, p.FileName, thumbSize);

            return string.Empty;
        }

        public static string GetThumbnailUrl(string galleryName, string fileName, int thumbSize)
        {
            return FileSystemGalleryManager.GetImageUrl(galleryName, fileName) + "&thumbSize=" + thumbSize;
        }

        public static string GetImageUrl(string galleryName, string fileName)
        {
            return "/image.aspx?galleryName=" + galleryName + "&fileName=" + fileName;
        }

        private static readonly string s_galleryDirectory = Config.DefaultInstance.GetConfigString("gallery", "imageDirectory");
        private static readonly string s_thumbsDirectory = Config.DefaultInstance.GetConfigString("gallery", "thumbsDirectory");
        private static readonly string s_thumbsFilePrefixFormat = Config.DefaultInstance.GetConfigString("gallery", "thumbsFilePrefixFormat");
    }
}
